<template>
    <basic-dialog :visible="show"
                  minWidth="400px"
                  title="服务期限"
                  :isShowClose="true"
                  :isFullScreen="false"
                  @close="hideDialog">
      <q-card-section>
        <q-table
          :data="data" :columns="columns"
          row-key="id" class="scroll myTable myTable-1 service-time-table"
          :loading="loading" table-style=""
          hide-bottom :pagination="{rowsPerPage: 0}"
          no-data-label="暂无数据">
        </q-table>
      </q-card-section>
      <q-card-section class="text-center margin-b-10">
        <q-icon name="iconfont iconpingtaitubiao-53" size="18px" style="margin-right: 9px" />服务电话：4006-021-808
      </q-card-section>
  </basic-dialog>
</template>

<script>
import { getServicePeriod } from 'src/boot/home/user'
export default {
  name: 'service-time-dialog',
  data () {
    return {
      show: false,
      data: [],
      loading: false,
      columns: [ // 所有标题
        { name: 'name', align: 'left', label: '模块', field: 'name', check: true },
        { name: 'value', align: 'left', label: '服务期限', field: 'value', check: true }
      ],
      allPrivileges: {} // 所有模块权限数据
    }
  },
  mounted () {
    this.getAllPrivileges()
  },
  methods: {
    // 显示弹框
    showDialog () {
      this.show = true
      // this.initInfo()
    },
    // 隐藏弹框
    hideDialog () {
      this.show = false
      this.data = []
    },
    // 初始化数据
    // initInfo () {
    //   let params = {
    //     clientType: 'pc'
    //   }
    //   this.loading = true
    //   getPrivileges(params).then(res => {
    //     this.loading = false
    //     res.data.forEach(item => {
    //       if (this.allPrivileges[item.id]) {
    //         this.data.push({
    //           name: this.allPrivileges[item.id],
    //           value: item.startDate + '~' + item.endDate
    //         })
    //       }
    //     })
    //   }).catch(() => {
    //     this.loading = false
    //   })
    // },
    // 获取服务期限
    getServicePeriod (userId) {
      getServicePeriod(userId).then(res => {
        this.loading = false
        console.log(res.data)
        res.data.forEach(item => {
          if (this.allPrivileges[item.privilegeId]) {
            this.data.push({
              name: this.allPrivileges[item.privilegeId],
              value: item.startDate && item.endDate ? item.startDate + '~' + item.endDate : '无限期'
            })
          }
        })
      }).catch(() => {
        this.loading = false
      })
    },
    // 获取所有权限数据
    getAllPrivileges () {
      Object.entries(this.Enums.SYS_PERMISSION)
        .filter(entry => (entry[1].type === 'MENU_LEVEL_ONE'))
        .map(item => item[1])
        .forEach(item => {
          this.allPrivileges[item.value] = item.desc
        })
    }
  }
}
</script>

<style scoped lang="stylus">
.myTable{
  margin 20px 0
  border-radius 6px
}
</style>
<style lang="stylus">
.service-time-table{
  .q-table__middle{
    max-height 500px
  }
}
</style>
